-- wait_timer
--
local import = import
---@type behavior_ret
local bret = import('bt/bt_plugin/behavior3/behavior_ret')
---@type WAIT_TIMER_TYPE
local WAIT_TIMER_TYPE = import('bt/custom_node/enum_type_cfg/WAIT_TIMER_TYPE')
local M = {
    name = "wait_timer",
    type = "Action",
    desc = "等待计时器结束",
    args = {
        {
            name = 'name',
            type = 'enum?',
            desc = '计时器类型',
            options = WAIT_TIMER_TYPE
        },
        { "waitTime", "int", "等待时间" },
        { "setDeltaTime", "int?", "间隔时间(毫秒)" }
    },
    input = { "计时器类型?" },
    doc = [[
        + 单位秒
    ]]
}

function M.run(node, env, value)
    local name = node.args.name
    local waitTime = node.args.waitTime
    local setDeltaTime = node.args.setDeltaTime
    local owner = env.owner
    local ctx = env.ctx
    local time = owner.nodeTimers[name]
    if not owner.nodeTimers[name] then
        owner.nodeTimers[name] = waitTime
    end
    if setDeltaTime then
        ctx:setDeltaTime(setDeltaTime)
    end
    if time <= 0 then
        owner.nodeTimers[name] = nil
        return bret.SUCCESS
    else
        time = time - ctx.realDeltaTime
        owner.nodeTimers[name] = time
        --xxmsg(string.format('【%s】计时器等待中 %s ---> %s', name, ctx.time, timer))
        return bret.RUNNING
    end
end

return M
